//
//  MBForumDetailHeaderView.m
//  MiBaoTu
//
//  Created by admin on 16/10/18.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "MBForumDetailHeaderView.h"

@interface MBForumDetailHeaderView () {
    UIView                  *_bgView;
    //头部
    UIView                  *_topView;
    UIImageView             *_userImgView;
    UILabel                 *_nickNameLb;
    UILabel                 *_timeLb;
    UIButton                *_deleteBt;
    UIImageView             *_topImgView;
    UIView                  *_lineView;
}

@end
@implementation MBForumDetailHeaderView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    //背景
    _bgView = [[UIView alloc]initWithFrame:CGRectZero];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    
    /*******************************头部*******************/
    _topView = [[UIView alloc]initWithFrame:CGRectZero];
    _topView.backgroundColor = [UIColor clearColor];
    [_bgView addSubview:_topView];
    //头像
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _userImgView.layer.cornerRadius = 40*PERCEN;
    _userImgView.layer.masksToBounds = YES;
    _userImgView.backgroundColor = [UIColor clearColor];
    [_topView addSubview:_userImgView];
    
    //昵称
    _nickNameLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _nickNameLb.backgroundColor = [UIColor clearColor];
    _nickNameLb.textAlignment = NSTextAlignmentLeft;
    _nickNameLb.textColor = UIColorFromHex(0x333333);
    _nickNameLb.font = [UIFont systemFontOfSize:14.f];
    if (ISIPAD) {
        _nickNameLb.font = [UIFont systemFontOfSize:30*PERCEN];
    }
    [_topView addSubview:_nickNameLb];
    
    //时间
    _timeLb = [[UILabel alloc]initWithFrame:CGRectZero];
    _timeLb.backgroundColor = [UIColor clearColor];
    _timeLb.textAlignment = NSTextAlignmentLeft;
    _timeLb.textColor = UIColorFromHex(0x999999);
    _timeLb.font = [UIFont systemFontOfSize:12.f];
    if (ISIPAD) {
        _timeLb.font = [UIFont systemFontOfSize:26*PERCEN];
    }
    [_topView addSubview:_timeLb];
    
    //删除
    _deleteBt = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteBt.backgroundColor = [UIColor clearColor];
    [_deleteBt setImage:[UIImage imageNamed:@"shanchu2"] forState:UIControlStateNormal];
    [_deleteBt setImage:[UIImage imageNamed:@"shanchu2"] forState:UIControlStateHighlighted];
    [_deleteBt addTarget:self action:@selector(onSelectForDelete) forControlEvents:UIControlEventTouchUpInside];
    _deleteBt.hidden = YES;
    [_topView addSubview:_deleteBt];
    
    //官方标志
    _topImgView = [[UIImageView alloc]initWithFrame:CGRectZero];
    _topImgView.backgroundColor = [UIColor clearColor];
    _topImgView.image = [UIImage imageNamed:@"guanfang"];
    _topImgView.hidden = YES;
    [_topView addSubview:_topImgView];
    
    //线
    _lineView = [[UIView alloc]initWithFrame:CGRectZero];
    _lineView.backgroundColor = [UIColorFromHex(0x999999)colorWithAlphaComponent:.15];
    [_topView addSubview:_lineView];
}

- (void)onSelectForDelete
{
    if (_delegate && [_delegate respondsToSelector:@selector(onSelectItemForDeleteUserActivityWithValue:)]) {
        [_delegate onSelectItemForDeleteUserActivityWithValue:_dataDic];
    }
}

- (void)setDataDic:(NSDictionary *)dataDic
{					
    if (!_dataDic) {
        _dataDic = [[NSDictionary alloc]init];
    }
    _dataDic = dataDic;
    [self setNeedsLayout];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    //top
    [_userImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",MUSIC_PLAY_Url,[_dataDic objectForKey:@"FaceImg"]]] placeholderImage:[UIImage imageNamed:@"xiaobian_bg"]];
    _nickNameLb.text = [_dataDic objectForKey:@"NickName"];
    _timeLb.text = [[NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"CreateTime"]] getChaTime];
    NSString *UserID = [NSString stringWithFormat:@"%@",[_dataDic objectForKey:@"UserID"]];
    UserModel *model = [UserManager standerDefult].userModel;
    if (!model) {
        _deleteBt.hidden = YES;
    }
    else if ([UserID isEqualToString:[NSString stringWithFormat:@"%@",model.UserID]]) {
        _deleteBt.hidden = NO;
    } else {
        _deleteBt.hidden = YES;
    }
    
    if (_isActivity) {
        _topImgView.hidden = NO;
        _nickNameLb.text = (_nickNameLb.text.length == 0 ? @"米宝兔官方帐号" : _nickNameLb.text);
    } else {
        _topImgView.hidden = YES;
    }
    
    //frame
    _bgView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 600*PERCEN);
    if (ISIPAD) {
        _bgView.frame = CGRectMake(0, 0, self.width, 600*PERCEN);

    }
    
    //top 120*percen
    _topView.frame = CGRectMake(0, 0, _bgView.width, (ISIPAD?140*PERCEN:120*PERCEN));
    _userImgView.frame = CGRectMake(25*PERCEN, (_topView.height-80*PERCEN)/2., 80*PERCEN, 80*PERCEN);
    if (ISIPAD) {
          _userImgView.frame = CGRectMake(0, 20*PERCEN, 100*PERCEN, 100*PERCEN);
    }
    _nickNameLb.frame = CGRectMake(_userImgView.right+20*PERCEN, _userImgView.top, 140, 20);
    if (ISIPAD) {
         _nickNameLb.frame = CGRectMake(_userImgView.right+20*PERCEN, _userImgView.top, 400*PERCEN, 30*PERCEN);
    }
    _timeLb.frame = CGRectMake(_nickNameLb.left, _userImgView.bottom-20, _nickNameLb.width, 20);
    if (ISIPAD) {
         _timeLb.frame = CGRectMake(_nickNameLb.left, _nickNameLb.bottom+30*PERCEN, _nickNameLb.width, 26*PERCEN);
    }
    UIImage *deleteImg = [UIImage imageNamed:@"shanchu"];
    _deleteBt.frame = CGRectMake(_topView.width-25*PERCEN-deleteImg.size.width, 30*PERCEN, deleteImg.size.width, deleteImg.size.height);
    if (ISIPAD) {
        _deleteBt.frame = CGRectMake(_topView.width-20*PERCEN-deleteImg.size.width, 30*PERCEN, deleteImg.size.width, deleteImg.size.height);
    }
    UIImage *topImg = [UIImage imageNamed:@"guanfang"];
    _topImgView.frame = CGRectMake(_deleteBt.left, _deleteBt.top, topImg.size.width, topImg.size.height);
    _lineView.frame = CGRectMake(_userImgView.left, 119*PERCEN, _topView.width-25*PERCEN, 1);
    if (ISIPAD) {
         _lineView.frame = CGRectMake(_userImgView.left, 139*PERCEN, _topView.width, 1);
    }
}

@end
